﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SSR;
using LitJson;
using System.IO;

namespace CardJsonEditor
{
    public partial class Form1 : Form
    {
        JsonData jsonObj;
        List<Card> cardMap;
        BindingSource bs = new BindingSource();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            String normalJson = "{\"cards\":[{\"name\":\"例子\",\"level\":4,\"p\":0}]}";
             if (!Directory.Exists(@"data\app\cc.heerdev.xunfang")) {
                Directory.CreateDirectory(@"data\app\cc.heerdev.xunfang");
                File.Create(@"data\app\cc.heerdev.xunfang\cards.json").Dispose();
                File.WriteAllText(@"data\app\cc.heerdev.xunfang\cards.json", normalJson );
            }
            if (!checkCardJsonInfo()) {
                File.Create(@"data\app\cc.heerdev.xunfang\cards.json").Dispose();
                File.WriteAllText(@"data\app\cc.heerdev.xunfang\cards.json", normalJson);
            }
            //初始化卡池json文件读取
            //确保卡池json存在，开始读取
            var jsonText = File.ReadAllText(@"data\app\cc.heerdev.xunfang\cards.json");
            if (jsonText == string.Empty)
            {
                jsonText = normalJson;
            }
            jsonObj = JsonMapper.ToObject(jsonText);
             cardMap = JsonMapper.ToObject<List<Card>>(jsonObj["cards"].ToJson());
            bs.DataSource = cardMap;
            listBox1.DataSource = bs;
            listBox1.DisplayMember = "name";
            update();
        }
        public bool checkCardJsonInfo() {
            if (File.Exists(@"data\app\cc.heerdev.xunfang\cards.json"))
            {

                return true;
            }
            else {
                return false;
            }
        }
        public void update() {
            try
            {
                listBox1.DisplayMember = null;
                listBox1.DisplayMember = "name";
                try
                {
                Card t = (Card)listBox1.SelectedItem;
                tbx_name.Text = t.name;
                tbx_level.Text = t.level.ToString();
                    tbx_p.Text = t.p.ToString();
                button_Add_Change.Text = "更改";

                }
                catch (Exception)
                {
                }
                jsonObj["cards"] = JsonMapper.ToObject(JsonMapper.ToJson(cardMap));
                File.WriteAllText(@"data\app\cc.heerdev.xunfang\cards.json", jsonObj.ToJson());
            }
            catch (Exception)
            {
                jsonObj["cards"] = JsonMapper.ToObject(JsonMapper.ToJson(cardMap));
                File.WriteAllText(@"data\app\cc.heerdev.xunfang\cards.json", jsonObj.ToJson());
                //MessageBox.Show("出现错误保存失败");
            }
                
        }

        private void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            update();
        }

        private void Button_Add_Change_Click(object sender, EventArgs e)
        {
            if (button_Add_Change.Text == "新增") {
                if (tbx_name.Text == string.Empty || tbx_level.Text == string.Empty)
                {
                    MessageBox.Show("请输入正确的名称与概率");
                    return;
                }
                cardMap.Add(new Card() { name = tbx_name.Text, level = int.Parse(tbx_level.Text), p = (tbx_p.Text==string.Empty)?0:int.Parse(tbx_p.Text.ToString()) });
                update();
                //listBox1.SelectedIndex = 0;
            }
            else
            {
                cardMap[listBox1.SelectedIndex].name = tbx_name.Text;
                cardMap[listBox1.SelectedIndex].level = int.Parse(tbx_level.Text);
                cardMap[listBox1.SelectedIndex].p = int.Parse(tbx_p.Text);
                update();
            }
        }

        private void Button_Add_Click(object sender, EventArgs e)
        {
            if (tbx_name.Text == string.Empty || tbx_level.Text == string.Empty) {
                MessageBox.Show("请输入正确的名称与概率");
                return;
            }
            cardMap.Add(new Card() { name = tbx_name.Text, level = int.Parse(tbx_level.Text), p = 0 });
            update();
            //listBox1.SelectedIndex = 0;

        }

        private void button2_Click(object sender, EventArgs e)
        {
            cardMap.Remove((Card)listBox1.SelectedItem);
            update();
        }

        private void button3_Click(object sender, EventArgs e)
        {

        }
    }
}
